import pyautogui
import pyperclip
import time
import datetime


def tab_and_fill(tab=0, fill="", copy=False, reverse=False):
    """
    tab:int，按几次TAB键
    fill:str，需要填充的字符
    copy：bool，是否复制到剪贴板
    reverse：bool，是否逆向
    
    """
    info_copyed = ''
    if reverse:
        for i in range(tab):
            pyautogui.hotkey('shift', 'tab')
    else:
        for i in range(tab):
            pyautogui.press('tab')
    if copy:
        pyautogui.hotkey('ctrl', 'c')
        info_copyed = pyperclip.paste()
    if fill:
        pyperclip.copy(fill)
        pyautogui.hotkey('ctrl', 'v')

    return info_copyed


def homepage(content):

    pyautogui.alert('准备开始！')
    today = time.strftime("%Y-%m-%d", time.localtime())
    now = f"{today} 10:00"
    pyautogui.PAUSE = 0.3

    screen_x, screen_y = pyautogui.size()
    pyautogui.click(int(screen_x / 2), int(screen_y / 2))
    for i in range(3):
        pyautogui.press('pgup')
    # 医疗付款方式
    tab_and_fill(2, "2")
    # 第几次住院
    tab_and_fill(2, "1")
    # 区县
    tab_and_fill(13, "？")
    # 居民身份证
    tab_and_fill(4, "居民身份证")
    # 复制现住址
    home_addr = tab_and_fill(4, "", True)
    # 邮编
    tab_and_fill(2, "473000")
    # 户口地址
    tab_and_fill(1, home_addr)
    # 邮编
    tab_and_fill(1, "473000")
    # 单位电话，邮编
    tab_and_fill(1, "-")
    tab_and_fill(1, "-")
    tab_and_fill(1, "-")
    # 入院途径
    tab_and_fill(5, "2")
    # 入院情况
    tab_and_fill(2, "一般")
    # 复制入院日期，计算确诊日期
    tab_and_fill(2)
    pyautogui.hotkey('ctrl', 'a')
    in_datetime = tab_and_fill(copy=True)
    in_datetime = datetime.datetime.strptime(in_datetime, '%Y-%m-%d %H:%M')
    two_days = datetime.timedelta(days=2)
    confirm_date = in_datetime + two_days

    confirm_date = datetime.datetime.strftime(confirm_date, '%Y-%m-%d')
    # 出院日期
    tab_and_fill(5)

    pyautogui.hotkey('ctrl', 'a')
    out_date = tab_and_fill(copy=True)
    print(out_date)
    if out_date == '-':
        tab_and_fill(fill=now)
    # 复制入院诊断
    dio = tab_and_fill(6, "", True).strip('？')
    dio = dio.strip('?')
    # 退回门急诊诊断并复制
    tab_and_fill(2, dio, reverse=True)
    # 拷贝疾病编码
    dio_code = tab_and_fill(1, "", True)
    time.sleep(0.5)
    # 粘贴编码
    tab_and_fill(2, dio_code)
    time.sleep(0.5)
    # 退回入院日期
    # 确诊日期
    tab_and_fill(2, confirm_date)
    time.sleep(0.5)
    # 删除末尾的-
    pyautogui.press('del')
    # 病情疗效
    tab_and_fill(2, '1')
    tab_and_fill(1, '1')
    while True:
        dio_second = tab_and_fill(2, copy=True)
        if dio_second == '-':
            break
        else:
            tab_and_fill(1, '1')
            tab_and_fill(1, '1')

    pyautogui.click(619, 873)
    # 入院与出院诊断
    tab_and_fill(2, '1')
    # 手术前后诊断--临床路径
    for code in [str(x) for x in [4, 4, 2, 2, 2, 2, 0, 0, 1, 2]]:
        tab_and_fill(1, code)
    # 尸检
    tab_and_fill(3, '2')
    # 血型--责任护士
    for each in ['6', '4', '陈芳', '王亚妮', '陈白璐', '陈白璐', '常佳']:
        tab_and_fill(1, each)
    # 病案质量
    tab_and_fill(4, '1')
    # 质控医师
    tab_and_fill(1, '陈芳')
    # 质控日期
    tab_and_fill(3, today)
    # 删除末尾的-
    pyautogui.press('del')

    # # 胃镜
    # tab_and_fill(1, content.setdefault('operation_code', '-'))
    # tab_and_fill(2, content.setdefault('operation_date', '-'))
    # pyautogui.press('del')
    # tab_and_fill(1, content.setdefault('operation_level', '-'))
    # tab_and_fill(1, content.setdefault('operation_sort', '-'))
    # tab_and_fill(1, content.setdefault('operation_sort', '-'))
    # tab_and_fill(2, content.setdefault('operation_operator', '-'))
    # tab_and_fill(3, content.setdefault('operation_cure_level', '-'))
    # tab_and_fill(1, content.setdefault('operation_anaesthetize', '-'))
    # tab_and_fill(1, content.setdefault('operation_anaesthetist', '-'))
    # tab_and_fill(1, content.setdefault('operation_anaesthetize_level', '-'))
    # 离院方式
    pyautogui.click(715, 624)
    tab_and_fill(2, '1')

    tab_and_fill(3, '1')
    tab_and_fill(2, '无')
    tab_and_fill(1, '2')
    tab_and_fill(2, '2')
    tab_and_fill(1, '2')
    # 日间手术
    pyautogui.click(499, 997)
    pyautogui.press('tab')
    pyautogui.click(177, 584)
    tab_and_fill(1, '2')
    pyautogui.click(662, 657)
    tab_and_fill(1, '0')
    tab_and_fill(1, '0')
    tab_and_fill(1, '0')
    tab_and_fill(5, '0')
    pyautogui.click(int(screen_x / 2), int(screen_y / 2))
    pyautogui.press('pgup')
    pyautogui.press('pgup')
    pyautogui.press('pgup')


if __name__ == "__main__":
    content = {}
    homepage(content)